package com.zhou.project.modules.circle.circleType.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.configurations.security.authentication.CurrentAuthentication;
import com.zhou.project.modules.circle.circleType.entity.CircleType;
import com.zhou.project.modules.circle.circleType.service.CircleTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("circle/type")
@Api(tags = "[圈子类型创建]")
public class CircleTypeController {

    @Autowired
    private CircleTypeService circleTypeService;
    @Autowired
    private CurrentAuthentication currentAuthentication;


    @PostMapping
    @ApiOperation("圈子创建")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "圈子名称",required = true,dataTypeClass = String.class)
    })
    @PreAuthorize("hasAuthority('circle:save')")
    //只有拥有管理员角色才能访问
//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    //拥有多个角色的时候 拥有任意一个角色都能访问
//    @PreAuthorize("hasAnyRole('ROLE_ADMIN','')")

    public void save(@RequestBody @ApiIgnore  @Valid CircleType entity)
    {
        circleTypeService.save(entity);
    }

    @PutMapping
    @ApiOperation("圈子修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "圈子id",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "name",value = "圈子名称",required = true,dataTypeClass = String.class)
    })
    @PreAuthorize("hasAuthority('circle:update')")
    public void update(@RequestBody @Valid CircleType entity)
    {
        circleTypeService.updateById(entity);
    }


    @ApiOperation("圈子删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "圈子id",required = true,dataTypeClass = Integer.class)
    })
    @DeleteMapping("{id}")
    @PreAuthorize("hasAnyAuthority('circle:delete')")
    public void delete(@PathVariable @NotNull(message = "主键id是必填项") int id)
    {
        circleTypeService.removeById(id);
    }


    @DeleteMapping("dbatch")
    @ApiOperation("圈子批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "List<CircleType>",value = "圈子ids",required = true,dataTypeClass = List.class)
    })
    @PreAuthorize("hasAuthorize('circle:dbatch')")
    public void delete(@RequestBody @NotNull(message = "主键id是必填项") List<Integer> ids)
    {
        circleTypeService.removeByIds(ids);
    }

    @GetMapping("page")
    @ApiOperation("圈子分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "圈子名称",required = false,dataTypeClass = String.class),
            @ApiImplicitParam(name = "current",value = "圈子当前页",required = false,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "size",value = "每页展示条数",required = false,dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('circle:page')")
    public Page<CircleType> query(@RequestBody CircleType entity)
    {

        entity.setCreatedBy(currentAuthentication.getCurrentUserId());
        return circleTypeService.page(entity);
    }

}
